<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="分类名称">
        <el-select v-model="sign" placeholder="请输入分类名称" size="small">
          <el-option v-for="item in categoryList" :key="item.id" :label="item.category_name" :value="item.sign"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" @click="search()" size="mini">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" :disabled="add" v-hasPermi="['manager:category:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['manager:category:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['manager:category:remove']">删除</el-button>
      </el-col>
    </el-row>

    <!-- 表格展示数据 -->
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="(item, index) in tableHead">
        <el-table-column :prop="item.column_name" :label="item.column_comment" v-if="item.column_name != 'id'" :key="index"></el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['manager:category:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:category:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页数据 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="search" />

    <!-- 添加或修改基础分类管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <template v-for="(item, index) in tableHead">
          <el-form-item :label="item.column_comment" :key="index" v-if="item.column_name != 'id'" :prop="item.column_name" :rules="[{ required: true, message: item.column_comment + '不能为空', trigger: 'blur' }]">
            <el-input v-model="form[item.column_name]" />
          </el-form-item>
        </template>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryList, getFacilitiesData, getTableHead, updateColumn, deleteColumn, insertColumn } from '@/api/basicInfo/category';
import { copy } from '@/Tools/index';
export default {
  name: 'Facilities',
  data() {
    return {
      // 遮罩层
      loading: true,
      categoryList: [],

      // 选中数组
      ids: [],
      // 非多个禁用
      add: true,
      multiple: true,
      single: true,
      // 弹窗名称
      title: '',
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      sign: '', // 分类
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },

      // 表头数据
      tableHead: [
        // {
        //   column_name: 'column_name',
        //   column_comment: '姓名'
        // },
        // {
        //   column_name: 'column_age',
        //   column_comment: '年龄'
        // },
        // {
        //   column_name: 'column_sex',
        //   column_comment: '性别'
        // }
      ],
      // 表格数据
      tableData: [
        // {
        //   column_age: '3',
        //   column_name: '鞠婧祎',
        //   column_sex: '女'
        // }
      ],
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询基础分类管理列表 */
    getList() {
      getCategoryList().then(response => {
        console.log(response);
        this.categoryList = response.data;

        // 判断是否有数据 有的话就把数据第一条选中
        if (response.data.length != 0) {
          this.sign = response.data[0].sign;
          this.add = false;
          this.search();
        } else {
          this.loading = false;
        }
      });
    },
    search() {
      this.loading = true;
      // 表头
      getTableHead(this.sign).then(res => {
        console.log(res);
        this.tableHead = res.data;
      });

      // 表数据
      getFacilitiesData(this.sign, this.queryParams).then(res => {
        console.log(res);
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = '新增';
      // 强制刷新组件
      this.$forceUpdate();
    },
    // 选中的数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1; // 修改
      this.multiple = !selection.length; // 删除

      // 修改数据回显
      if (!this.single) {
        this.form = copy(selection[0]);
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = '修改';

      if (row.id) {
        this.form = copy(row);
      }
    },
    // 删除
    handleDelete(row) {
      this.$confirm(`是否确认删除数据`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(row.id || this.ids.join(','));
          return deleteColumn({
            sign: this.sign,
            ids: row.id || this.ids.join(',')
          });
        })
        .then(() => {
          this.search();
          this.msgSuccess('删除成功');
        });
    },
    // 提交按钮（新增和修改）
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            // 把类型追加到数据里面;
            this.form.sign = this.sign;
            updateColumn(this.form).then(res => {
              console.log(res);
              this.msgSuccess('修改成功');
              this.open = false;
              this.form = {};
              this.search();
            });
          } else {
            // 新增
            this.form.sign = this.sign;
            insertColumn(this.form).then(res => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.form = {};
              this.search();
            });
          }
        }
      });
    },
    // 取消弹窗按钮
    cancel() {
      this.open = false;
      this.form = {};
      // 重置表单校验
      this.$refs['form'].resetFields();
      // 强制刷新组件
      this.$forceUpdate();
    },
    // 点击关闭弹窗按钮
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
          // 清除表单里面的内容
          this.form = {};
          // 重置表单校验
          this.$refs['form'].resetFields();
          // 强制刷新组件
          this.$forceUpdate();
        })
        .catch(_ => {});
    }
  }
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  /* overflow: hidden; */
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.avatar {
  width: 50px;
  height: 50px;
  display: block;
}

.imgsty {
  position: absolute;
  top: -5px;
  right: -5px;
  background: red;
  /* padding: 4px; */
  color: #fff;
  border-radius: 10px;
}
</style>
